package com.khstudio.api.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

public class VideoPlayButton_Kh extends View {
    public static final int PLAY  = 0x0001;
    public static final int PAUSE = 0x0002;

    private Context     context;
    private int         state;

    private Drawable    drawablePlay;
    private Drawable    drawablePause;

    private int         drawableWidth;
    private int         drawableHeight;

    private VideoPlayButtonClickListener mListener;

    public interface VideoPlayButtonClickListener {
        void onClickListener(int s);
    }

    public VideoPlayButton_Kh(Context context) {
        this(context, null);
    }

    public VideoPlayButton_Kh(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VideoPlayButton_Kh(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        this.context = context;
        _init(attrs, defStyle);
    }

    public void setState(int s) {
        if(state != s) {
            state = s;
            invalidate();
        }
    }

    public int getState() {
        return state;
    }

    public int getDrawableWidth() {
        return drawableWidth;
    }

    public int getDrawableHeight() {
        return drawableHeight;
    }

    public void setOnVideoPlayButtonClickListener(VideoPlayButtonClickListener l) {
        mListener = l;
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mListener != null) mListener.onClickListener(state);

                if(state == PLAY) {
                    setState(PAUSE);
                } else {
                    setState(PLAY);
                }
            }
        });
    }

    public void setDrawableOnId(int id) {
        Drawable d = context.getResources().getDrawable(id);
        setDrawablePlay(d);
    }

    public void setDrawableOffId(int id) {
        Drawable d = context.getResources().getDrawable(id);
        setDrawablePause(d);
    }

    public void setDrawablePlay(Drawable d) {
        drawablePlay = d;
        drawablePlay.setCallback(this);
        drawablePlay.setState(getDrawableState());
        drawablePlay.setVisible(getVisibility() == VISIBLE, false);

        if(drawablePause != null) {
            int width = drawablePlay.getIntrinsicWidth();
            int height = drawablePlay.getIntrinsicHeight();
            drawableWidth = drawableWidth > width ? drawableWidth : width;
            drawableHeight = drawableHeight > height ? drawableHeight : height;
        } else {
            drawableWidth = drawablePlay.getIntrinsicWidth();
            drawableHeight = drawablePlay.getIntrinsicHeight();
        }
    }

    public void setDrawablePause(Drawable d) {
        drawablePause = d;
        drawablePause.setCallback(this);
        drawablePause.setState(getDrawableState());
        drawablePause.setVisible(getVisibility() == VISIBLE, false);

        if(drawablePlay != null) {
            int width = drawablePause.getIntrinsicWidth();
            int height = drawablePause.getIntrinsicHeight();
            drawableWidth = drawableWidth > width ? drawableWidth : width;
            drawableHeight = drawableHeight > height ? drawableHeight : height;
        } else {
            drawableWidth = drawablePause.getIntrinsicWidth();
            drawableHeight = drawablePause.getIntrinsicHeight();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(_getMeasureSize(widthMeasureSpec, drawableWidth), _getMeasureSize(heightMeasureSpec, drawableHeight));
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        if(changed) {
            int width = right - left;
            int height = bottom - top;

            int l;
            int t;
            int r;
            int b;

            if( width > drawableWidth ) {
                l = ( width - drawableWidth ) / 2;
                r =  l + drawableWidth;
            } else {
                l = 0;
                r = width;
            }

            if( height > drawableHeight ) {
                t = ( height - drawableHeight ) / 2;
                b =  t + drawableHeight;
            } else {
                t = 0;
                b = height;
            }

            if(drawablePlay != null) drawablePlay.setBounds(l, t, r, b);
            if(drawablePause != null) drawablePause.setBounds(l, t, r, b);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(state == PLAY) {
            if(drawablePlay != null) drawablePlay.draw(canvas);
        } else {
            if(drawablePause != null) drawablePause.draw(canvas);
        }
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();

        int[] state = getDrawableState();

        if (drawablePlay != null && drawablePlay.isStateful()) {
            drawablePlay.setState(state);
        }

        if (drawablePause != null && drawablePause.isStateful()) {
            drawablePause.setState(state);
        }

        invalidate();
    }

    private void _init(AttributeSet attrs, int defStyle) {
        state = PLAY;

        mListener = null;

        drawablePlay = null;
        drawablePause = null;

        drawableWidth = 0;
        drawableHeight = 0;

        if(attrs != null) {
            int drawablePlayW  = 0, drawablePlayH  = 0;
            int drawablePauseW = 0, drawablePauseH = 0;

            int[] ids = new int[attrs.getAttributeCount()];
            for (int i = 0; i < attrs.getAttributeCount(); i++) {
                ids[i] = attrs.getAttributeNameResource(i);
            }

            TypedArray a = context.obtainStyledAttributes(attrs, ids, defStyle, 0);

            for (int i = 0; i < attrs.getAttributeCount(); i++) {
                String attrName = attrs.getAttributeName(i);
                if (attrName == null)
                    continue;

                if (attrName.equals("btn_play")) {
                    drawablePlay = a.getDrawable(i);
                    drawablePlay.setCallback(this);
                    drawablePlay.setState(getDrawableState());
                    drawablePlay.setVisible(getVisibility() == VISIBLE, false);
                    drawablePlayW = drawablePlay.getIntrinsicWidth();
                    drawablePlayH = drawablePlay.getIntrinsicHeight();
                } else if (attrName.equals("btn_pause")) {
                    drawablePause = a.getDrawable(i);
                    drawablePause.setCallback(this);
                    drawablePause.setState(getDrawableState());
                    drawablePause.setVisible(getVisibility() == VISIBLE, false);
                    drawablePauseW = drawablePause.getIntrinsicWidth();
                    drawablePauseH = drawablePause.getIntrinsicHeight();
                }
            }
            a.recycle();

            drawableWidth = drawablePlayW > drawablePauseW ? drawablePlayW : drawablePauseW;
            drawableHeight = drawablePlayH > drawablePauseH ? drawablePlayH : drawablePauseH;
        }
    }

    private int _getMeasureSize(int measureSpec, int cSize) {
        int measureMode = MeasureSpec.getMode(measureSpec);
        int measureSize = MeasureSpec.getSize(measureSpec);
        int size = cSize;

        if(measureMode == MeasureSpec.EXACTLY){
            size = measureSize;
        } else if(measureMode == MeasureSpec.UNSPECIFIED || measureMode == MeasureSpec.AT_MOST){
            // Check against our minimum width
            size = Math.max(size, getSuggestedMinimumWidth());

            if (measureMode == MeasureSpec.AT_MOST) {
                size = Math.min(measureSize, size);
            }
        }

        return size;
    }
}
